/*    file: main.cpp
 *    desc:
 * 
 * created: 2016-04-06
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include <stdio.h>
#include <iostream>

#include "BasicType.h"
#include "SharedPtr.h"
#include "TFileUtil.h"
#include "TByteBuffer.h"
#include "BasicMacro.h"
#include "TStringUtil.h"
#include <assert.h>
#include <errno.h>
#include "RecordStore.h"
#include "DateTime.h"
#include "Path.h"


class Application 
{
public:
	Application()
	{
	}

	~Application()
	{
	}

};



void testQuery(av::RecordStore& RecordStore)
{
	printf("query record ... \n");

	av::MRecordQuery query = av::MRecordQuery();
	query.fromTime = 0;
	query.toTime   = 0;// fromTime + 24 * 60 * 60;

	av::MRecord records[200];
	int count = RecordStore.findRecord(query, 0, records, ARRAY_SIZE(records));
	for (int i = 0; i < count; i++)
	{
		printf("record. id:%d, chl:%s, duration:%d, name:%s\n",
			records[i].id, records[i].device, 
			records[i].duration, records[i].filepath);
	}

	if (count > 0)
	{
		RecordStore.deleteRecord(records[0].id);
	}
	printf("\n");
}


int main(int argc, char** argv)
{
	std::string workdir = comn::Path::getWorkDir();
	std::string filename = comn::Path::join(workdir, "MediaStorage.s3db");

	//rtsp://192.168.1.230/main
	//f:\media\ps.ps
	// rtsp://192.168.2.105/demo

	if (argc > 1)
	{
		filename = argv[1];
	}

	Application app;

	std::shared_ptr< av::RecordStore> RecordStore(av::createRecordStore(), av::destroyRecordStore);

	int rc = RecordStore->open(filename.c_str());
	if (rc != 0)
	{
		printf("failed to open storage db. %s\n", filename.c_str());
		return 0;
	}

	testQuery(*RecordStore.get());

	if (false)
	{
		RecordStore->clearBrokenRecord(0);
		std::cin.get();
		RecordStore->close();
		return 0;
	}


	RecordStore->close();

	return 0;
}




